package com.rhb.ca.uzer.dao;

import java.util.*;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rhb.ca.util.UID;
import com.rhb.ca.uzer.bean.Uzer;
import com.rhb.ca.uzer.bean.UzerQuery;

public class UzerDAO_HibernateImpl extends HibernateDaoSupport implements UzerDAO {
	static Logger logger = Logger.getLogger(UzerDAO_HibernateImpl.class);
	
	public Uzer selectUzerByLoginName(String loginName){
		//System.out.println("********UzerDAO_HibernateImpl.selectUzerByLoginName()");
		String hql = "from Uzer uzer where loginName=:loginName";
		Query query = this.getSession().createQuery(hql);
		query.setString("loginName", loginName);
		List list = query.list();
		if(list.isEmpty()){
			return null;
		}else{
			Uzer uzer = (Uzer)list.get(0);
			return (Uzer)this.getHibernateTemplate().get(Uzer.class, uzer.getUzerPk());
		}
	}

	public void delete(Uzer obj) {
		try {
			this.getHibernateTemplate().delete(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Uzer findByPK(String pk) {
		return (Uzer) this.getHibernateTemplate().get(Uzer.class, pk);
	}

	public String create(Uzer obj) {
		//String pk = UID.getUID();
		//obj.setUzerPk(pk);
		try {
			this.getHibernateTemplate().save(this.getHibernateTemplate().merge(obj));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj.getUzerPk();
	}

	public void update(Uzer obj) {
		try {
			this.getHibernateTemplate().update(this.getHibernateTemplate().merge(obj));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Query getQuery(String str,UzerQuery quester){
		StringBuffer hql = new StringBuffer(str);
		if (quester != null) {
											if (quester.getUzerName() != null && !"".equals(quester.getUzerName())) {
					hql.append(" and uzerName like :uzerName");
				}
								
															if (quester.getLoginName() != null && !"".equals(quester.getLoginName())) {
					hql.append(" and loginName like :loginName");
				}
								
									}

		Query query = this.getSession().createQuery(hql.toString());
		if (quester != null) {
											if (quester.getUzerName() != null && !"".equals(quester.getUzerName())) {
					query.setString("uzerName","%"+String.valueOf(quester.getUzerName())+"%");
				}
								
															if (quester.getLoginName() != null && !"".equals(quester.getLoginName())) {
					query.setString("loginName","%"+String.valueOf(quester.getLoginName())+"%");
				}
								
									}
		return query;
	}

	public int getAllCount_findByQuery(UzerQuery quester) {
		Long allcount = new Long(0);
		String hql = "select count(uzer) from Uzer uzer where 1=1";
		try {
			Query query = getQuery(hql,quester);
			Iterator result = query.iterate();
			if (result.hasNext()) {
				allcount = (Long) result.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allcount.intValue();
	}

	public List findByQuery(UzerQuery quester, int start, int count) {
		String hql = "select uzer from Uzer uzer where 1=1";
		try {
			Query query = getQuery(hql,quester);
			query.setFirstResult(start);
			query.setMaxResults(count);

			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Uzer findByLoginName(String no) {
		List list = null;
		String hql_find_by_no = "from Uzer where loginName=?";
		try {
			list = this.getHibernateTemplate().find(hql_find_by_no, no);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (list != null && list.isEmpty()) {
			return null;
		} else {
			Iterator it = list.iterator();
			Uzer uzer = (Uzer) it.next();
			return uzer;
		}
	}
	public boolean existLoginName(String no, String pk) {
		boolean flag = false;
		if (no == null || "".equals(no.trim())) {
			flag = false;
		} else {
			Long count = new Long(0);
			StringBuffer hql = new StringBuffer(
					"select count(uzer) from Uzer uzer where loginName='"
							+ no + "'");

			if (pk != null && !"".equals(pk.trim())) {
				hql.append(" and uzerPk != '" + pk + "'");
			}

			try {
				Iterator result = this.getHibernateTemplate().find(
						hql.toString()).iterator();
				if (result.hasNext()) {
					count = (Long) result.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (count.equals(new Long(0))) {
				flag = false;
			} else {
				flag = true;
			}
		}
		return flag;
	}

}
